<?php

namespace App\Http\Controllers\ChatGroup\Web;

use App\Http\Controllers\BaseController;
use App\Models\ChatGroup\ChatGroupFAQModel;
use App\Service\BaseService;
use App\Service\ChatGroup\ChatGroupFAQService;
use App\Service\ChatGroup\ChatGroupService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class FaqController extends BaseController
{
    /**
     * 常见问题
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $pageSize  = (int)$request->get('pageSize', 20);
        $pageNum   = (int)$request->get('pageNum', 1);
        $titleName = (string)$request->get('title');

        $list = ChatGroupFAQService::getFAQLevel3List($pageSize, $pageNum, $titleName);
        return $this->success($list);
    }

    /**
     * 删除常见问题
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function del(Request $request): JsonResponse
    {
        $id = (int)$request->get('id');
        ChatGroupFAQService::del($id);

        dispatch(function () {
            ChatGroupFAQService::faqRefreshUpdateSort();
        });
        return $this->success();
    }

    /**
     * 常见问题排序列
     * @param Request $request
     * @return JsonResponse
     */
    public function sortList(Request $request): JsonResponse
    {
        $level = (int)$request->get('level', 1);
        $pid   = (int)$request->get('pid', 0);

        $list = ChatGroupFAQService::faqSortList($level, $pid);
        return $this->success($list->toArray());
    }


    /**
     * 更新排序
     *
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function updateSort(Request $request): JsonResponse
    {
        $sort = (int)$request->get('sort');
        $id   = (int)$request->get('id');

        ChatGroupFAQService::faqUpdateSort($id, $sort);
        return $this->success([
            'msg' => '已触发更新排序, 稍后刷新页面获取最新排序'
        ]);
    }

    /**
     * 常见问题详情
     *
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function info(Request $request): JsonResponse
    {
        $id   = (int)$request->get('id');
        $info = ChatGroupFAQService::info($id);

        return $this->success($info);
    }


    /**
     * 更新数据
     *
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function faqUpdate(Request $request): JsonResponse
    {
        $id                = (int)$request->get('id');
        $parentTitle       = (string)$request->get('parentTitle');
        $parentParentTitle = (string)$request->get('parentParentTitle');
        $desc              = (string)$request->get('desc');
        $indexNumber       = (string)$request->get('indexNumber');
        $description       = (string)$request->get('description');
        $answer            = (string)$request->get('answer');
        $images            = $request->get('images');
        $videos            = $request->get('videos');

        ChatGroupFAQService::faqUpdate($id, $parentTitle, $parentParentTitle, $desc, $indexNumber, $description,
            $answer, $images, $videos);
        return $this->success();
    }

    /**
     * 文件导入, 校验
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function fileCheck(Request $request): JsonResponse
    {
        $file     = $request->file('file');
        $isImport = (bool)$request->get('isImport');

        $allowed_extensions = ['csv', 'xlsx', 'xls'];
        if (empty($file) || !in_array(strtolower($file->getClientOriginalExtension()), $allowed_extensions)) {
            BaseService::throwErr('只能上传 ' . implode(',', $allowed_extensions) . ' 格式的文件.');
        }

        list($errorMsg, $importResult) = ChatGroupFAQService::fileCheckAndImport($file, $isImport);

        return $this->success([
            'isSuccess' => $importResult,
            'returnMsg' => $errorMsg,
        ]);
    }

    /**
     * 工单功能开通获取
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getWorkOrderUrl(Request $request): JsonResponse
    {
        $problemPid = (int)$request->get('problemPid', 0);

        return $this->success([
            'list' => ChatGroupFAQService::getFaqLinkUrl($problemPid)
        ]);
    }

    /**
     * 下载模版
     *
     * @return JsonResponse
     */
    public function downloadTemplate(): JsonResponse
    {
        return $this->success([
            'downloadUrl' => 'https://file.51kuafu.com/83/c5/9996e154ad2f89e17683d712d55773553538b01a70cd4bf5658480faa8fc.xlsx',
        ]);
    }

}
